define(['app'], function (app) {
    app.controller('BookClassifyCtrl', ['$scope', '$location', '$uibModal', 'categoryService',
        function ($scope, $location, $uibModal, categoryService) {

            //代码书写步骤
            //1.用【$scope】定义公共类model,和页面绑定的model
            //2.用【var】定义controller内部用的变量
            //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
            //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
            //定义规则，按可用性分忧优先级，公共的在前，private的在后

            //页面查询的数据
            $scope.bookClassifyData = [];
            //$scope.allData=[];

            //打开modal框
            $scope.openAddModel = function (type, item) {
                var modalInstance = $uibModal.open({
                    animation: true,
                    templateUrl: 'myModalContent.html',
                    controller: 'BookClassifyAddCtrl',
                    size: '',//lg,sm, 默认大小
                    resolve: {
                        items: function () {//页面之间传递参数时使用
                            var items = { type: type, item: item };
                            return items;
                        }
                    }
                });
                //响应modal关闭时的处理方法
                modalInstance.result.then(function () {
                    $scope.getCategoryList();
                }, function () {
                    // $log.info('Modal dismissed at: ' + new Date());
                });
            };

            //模糊搜索
            $scope.search = function () {
                if ($scope.searchName == null || $scope.searchName == '') {
                    $scope.getCategoryList();
                }
                else {
                    var q = { name: $scope.searchName };
                    categoryService.query(q).then(
                        function (response) {
                            //if (response != null && response.length > 0) {
                            $scope.bookClassifyData = response;
                            //$scope.allData = response;
                            //} else {
                            //    alert("查询数据失败!");
                            //}
                        },
                        function (error) {
                            alert("查询数据失败！");
                        }
                    );
                }
            };

            //删除分类
            $scope.deleteModel = function (item) {
                categoryService.deleteCategory(item.id).then(
                        function (response) {
                            if (response > 0) {
                                alert("删除成功！");
                                $scope.getCategoryList();

                            } else {
                                alert("删除失败！");
                            }
                        },
                        function (code) {
                            alert("删除失败！");
                        }
                    );
            };
            //删除提示框
            $scope.deleteConfirm = function (item) {
                var result = confirm('是否删除！');
                if (result) {
                    $scope.deleteModel(item);
                } else {
                    alert('不删除！');
                }
            }
            //首次加载，获取所有图书分类信息
            $scope.getCategoryList = function () {
                categoryService.getCategoryList().then(
                    function (response) {
                        if (response != null && response.length > 0) {
                            $scope.bookClassifyData = response;
                            //$scope.allData=  response;
                        } else {
                            alert("查询数据失败!");
                        }
                    },
                    function (error) {
                        alert("查询数据失败！");
                    }
                );
            };
            //调用获取图书分类方法
            $scope.getCategoryList();

            //初始化
            var init = function () {
                console.log("BookClassifyCtrl init");
                $scope.dashboardModel.isActiveMenu = 'classify';//左侧菜单按钮赋初值
            };
            init();

            //深拷贝
            var cloneObj = function (obj) {
                var str, newobj = obj.constructor === Array ? [] : {};
                if (typeof obj !== 'object') {
                    return;
                } else if (window.JSON) {
                    str = JSON.stringify(obj), //系列化对象
                        newobj = JSON.parse(str); //还原
                } else {
                    for (var i in obj) {
                        newobj[i] = typeof obj[i] === 'object' ?
                            cloneObj(obj[i]) : obj[i];
                    }
                }
                return newobj;
            };

        }]);
});